package com.inhg.inhg_android.net;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Allen on 2017/5/3.
 * <p>
 * RetrofitClient工具类
 */

public class RetrofitClient {

    private static RetrofitClient instance;

    private Retrofit.Builder mRetrofitBuilder;
    private OkHttpClient.Builder mOkHttpBuilder;

    private final String BASE_URL = "http://test.api.maka.im/";

    public static RetrofitClient getInstance() {

        if (instance == null) {
            synchronized (RetrofitClient.class) {
                if (instance == null) {
                    instance = new RetrofitClient();
                }
            }

        }
        return instance;
    }


    public RetrofitClient() {
        initOkHttpBuilder();
        /*初始化retrofit的builder*/
        mRetrofitBuilder = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//加入rxjava调用方式
                .addConverterFactory(GsonConverterFactory.create())//gson 解析库
                .client(mOkHttpBuilder.build());
    }

    /**
     * 初始化okhttpBuidler
     */
    private void initOkHttpBuilder() {
        mOkHttpBuilder = new OkHttpClient().newBuilder();
        mOkHttpBuilder.readTimeout(10, TimeUnit.SECONDS);
        mOkHttpBuilder.connectTimeout(9, TimeUnit.SECONDS);
    }

    /**
     * @return 得到retrofit
     */
    public Retrofit getRetrofit() {
        return mRetrofitBuilder.client(mOkHttpBuilder.build()).build();
    }

}
